package de.tum.antiTheft;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.widget.Toast;

public class AntiTheft extends Activity {
	private WakeLock wakeLock;
	
//	<receiver android:name=".AntiTheft" android:enabled="true"
//	        android:exported="true">
//	        <intent-filter>
//	            <action android:name="android.intent.action.UMS_DISCONNECTED" />
//	        </intent-filter>
//		</receiver>
	
	/** receiver to get an event when USB is unplugged */
    private BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction() == Intent.ACTION_POWER_DISCONNECTED) {
        		Toast.makeText(AntiTheft.this,  "intent received" ,Toast.LENGTH_LONG).show();
                wakeLock.acquire(); // screen will stay on
        		MediaPlayer mp = MediaPlayer.create(context, R.raw.sheep);
        		mp.start();
            }
        }
    };

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        registerReceiver(receiver, new IntentFilter(Intent.ACTION_POWER_DISCONNECTED));
        PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
        wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag");
		MediaPlayer mp = MediaPlayer.create(this, R.raw.sheep);
		mp.start();
    }
}